package com.medicine.hbp.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.medicine.hbp.base.MyBaseAdapter;
import com.medicine.hbp.mob.ArtMeInfoVo;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.R;

/**
 * @author Administrator
 * 
 */
public class ArtMeAdapter extends MyBaseAdapter {
	private LayoutInflater inflater;
	private Activity activity;

	public ArtMeAdapter(Activity activity) {
		this.activity = activity;
		inflater = LayoutInflater.from(activity);
	}

	public void addItem(ArtMeInfoVo items) {
		notifyDataSetChanged();
		List<ArtMeInfoVo> infoVos=new ArrayList<ArtMeInfoVo>();
		infoVos.add(items);
		infoVos.addAll(getItems());
		setItems(infoVos);
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2) {
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = inflater.inflate(R.layout.feedback_item, null);
			holder.text_you_detail = (TextView) view
					.findViewById(R.id.text_you_detail);
			holder.text_you_time = (TextView) view
					.findViewById(R.id.text_you_time);
			holder.text_my_detail = (TextView) view
					.findViewById(R.id.text_my_detail);
			holder.text_my_time = (TextView) view
					.findViewById(R.id.text_my_time);
			holder.ll_you = (LinearLayout) view.findViewById(R.id.ll_you);
			holder.ll_my = (LinearLayout) view.findViewById(R.id.ll_my);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		WindowManager manager = activity.getWindowManager();
		int width = manager.getDefaultDisplay().getWidth() / 4 * 3;
		ArtMeInfoVo infoVo = (ArtMeInfoVo) getItem(getCount() - 1
				- position);
		if (infoVo != null) {
			if (LoginSp.getUserId(activity) == infoVo.userId) {
				holder.ll_you.setVisibility(View.GONE);
				holder.ll_my.setVisibility(View.VISIBLE);
				holder.text_my_detail.setText(infoVo.msgInformation);
//				holder.text_my_time.setText(infoVo.msgDate.substring(0, infoVo.msgDate.length()-5));
			} else {
				holder.ll_you.setVisibility(View.VISIBLE);
				holder.ll_my.setVisibility(View.GONE);
				holder.text_you_detail.setText(infoVo.msgInformation);
//				holder.text_you_time.setText(infoVo.opinionDate.substring(0, infoVo.opinionDate.length()-5));
			}
			holder.text_you_detail.setMaxWidth(width);
			holder.text_my_detail.setMaxWidth(width);
		}
		return view;
	}

	class ViewHolder {
		LinearLayout ll_you;
		LinearLayout ll_my;
		TextView text_you_detail;
		TextView text_you_time;
		TextView text_my_detail;
		TextView text_my_time;
	}

}
